package com.qf.kele02.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author: 可乐
 * @Description:
 * @CreateTime: 2022-10-18  19:43
 */

@RestController
@RequestMapping("/upload")
@Slf4j
public class upload02 {
	@PostMapping("/test1")
	@ResponseBody
	public String upload( @RequestParam("file") MultipartFile file ) {
		if(file.isEmpty( )) {
			return "上传失败，请选择文件";
		}

		String fileName=file.getOriginalFilename( );
		// 上传文件路径的处理，相对项目jar包的部署路径存放upload目录
		String userDir=System.getProperty( "user.dir" );
		String filePath=userDir+File.separator+"upload";
		File dir=new File( filePath );
		if(!dir.exists( )) {
			dir.mkdir( );
		}
		String dest=filePath+File.separator+fileName;
		log.debug( "dest={}",dest );
		try {
			file.transferTo( new File( dest ) );
			log.info( "上传成功" );
			return "上传成功";
		} catch(IOException e) {
			log.error( e.toString( ),e );
		}
		return "上传失败！";
	}
}
